import ACCESS_ENUMS from "@/access/accessEnums";
import { UserControllerService } from "../../../generate";

const state = () => ({
  loginUser: {
    userName: "未登录",
  },
});

// getters
const getters = {};

// actions
const actions = {
  async getLoginUser({ commit, state }: any, payload: any) {
    //远程获取用户信息
    const res = await UserControllerService.getLoginUserUsingGet();
    if (res.code === 0) {
      commit("updateUser", res.data);
    } else {
      commit("updateUser", {
        ...state.loginUser,
        userRole: ACCESS_ENUMS.NOT_LOGIN,
      });
    }
  },
};

// mutations
const mutations = {
  updateUser(state: { loginUser: any }, user: any) {
    state.loginUser = user;
  },
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
};
